﻿using AvaloniaApplicationDemo.DTO;
using AvaloniaApplicationDemo.Models;
using Microsoft.Extensions.DependencyInjection;
using SukiUI.Dialogs;
using SukiUI.Toasts;

namespace AvaloniaApplicationDemo.Util;

/// <summary>
/// Service拓展类
/// </summary>
public static class ServiceCollectionExtensions
{
    /// <summary>
    /// 使用Scrutor进行Service注册
    /// </summary>
    /// <param name="container"></param>
    public static void AddServiceWithScrutor(this IServiceCollection container)
    {
        // 注册 ViewModels
        container.Scan(scan => scan
            .FromAssemblyOf<Program>()
            .AddClasses(cls => cls.Where(type => type.Namespace.Contains("ViewModels")))
            .AsSelf()
            .WithSingletonLifetime()
        );
        AppLogger.Information("ViewModels Registration completed");
        // 注册 Views
        container.Scan(scan => scan
            .FromAssemblyOf<Program>()
            .AddClasses(cls => cls.Where(type => type.Namespace.Contains("Views")))
            .AsSelf()
            .WithTransientLifetime()
        );
        AppLogger.Information("Views Registration completed");
        // 注册 Manager
        container.Scan(scan => scan
            .FromAssemblyOf<Program>()
            .AddClasses(cls => cls.Where(type => type.Name.EndsWith("Manager")))
            .AsImplementedInterfaces()
            .WithSingletonLifetime()
        );
        container.AddTransient<SlaveInfo>();
        container.AddSingleton<DeviceModel>();
        container.AddSingleton<ModbusReportModel>();
        //注册特殊的Service

        container.AddSingleton<ISukiDialogManager, SukiDialogManager>();
        container.AddSingleton<ISukiToastManager, SukiToastManager>();


        container.AddSingleton<ViewLocator>(); // 注册 ViewLocator用于根据ViewModel查找对应的View

        //container.AddSingleton<ThemeService>();
    }
}